<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2021/4/28
 * Time: 10:13
 */

namespace pay\plug;


use pay\forms\fontend\order\OrderForm;
use pay\libs\CheckSign;
use pay\libs\JudgeSign;
use pay\plug\constructionBank\CcbMain;
use yii\base\Exception;

class CcbMainPay extends CcbMain
{
    public $data;               // 解密后的参数
    public $aid;
    public $signType;          // 加密类型
    public $paramsData;        // post 参数

    /*** 发送支付请求
     * @return false|string
     */
    public function sendPay()
    {
        $this->checkParams();
        $payParams = $this->submitOrder();
        if ($this->data['amount'] ==  0){
            return '价格0元，调起支付失败';
        }
        if ($payParams['code'] == 0){
            $result = $this->sendPayParams($payParams['msg']);
            return $result;
        }else{
            throw new Exception($payParams['msg'],$payParams['code']);
        }

    }


    /*** 校验、解密参数
     * @return array
     * @throws Exception
     */
    public function checkParams()
    {
        $checkSign = (new CheckSign()); // 校验签名是否正确
        $paramsData = $checkSign->checkSignWxPay($this->paramsData);
        $this->data = $paramsData;
        return $this->data;
    }


    /*** 创建订单
     * @return array
     * @throws Exception
     */
    public function submitOrder()
    {
        $form = new OrderForm();
        $form->form_data = $this->data;
        return $form->submit();
    }

    /*** 发起建行支付请求
     * @param $payParams
     * @return false|string
     * @throws Exception
     */
    public function sendPayParams($payParams)
    {
        $this->orderId = $payParams['pay_order_sn'];
        $this->payMent = $payParams['amount']/100;      // 建行支付单位为元
        $this->remark1 = $payParams['channelMerId'];
        $this->remark2 = $payParams['aid'];
        $this->clientIp = \Yii::$app->request->userIP;
        $this->tradeType = 'MINIPRO';
        $this->subAppid = $payParams['channelId'];
        $this->subOpenid = $payParams['userId'];
        return $this->getPayData();
    }


    /*** 判断回调参数是否缺失
     * @param $params
     * @return mixed
     * @throws Exception
     */
    public function callBackData($params)
    {

        if ($params['POSID'] != $this->POSID){
            throw new Exception('回调信息有误-'.$params['POSID']);
        }
        if ($params['BRANCHID'] != $this->BRANCHID){
            throw new Exception('回调信息有误-'.$params['BRANCHID']);
        }
        if (empty($params['ORDERID']) || empty($params['PAYMENT']) || empty($params['REMARK1'])){
            throw new Exception('回调信息有误-'.json_encode($params,JSON_UNESCAPED_UNICODE));
        }
        return $params;
    }



    public function getFromServer($address,$service_port,$send_data)
    {
        var_dump($address,$service_port,$send_data);die;
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if ($socket < 0) {
            echo "socket´´½¨Ê§°ÜÔ­Òò: " . socket_strerror($socket) . "\n";
        } else {
            echo "socket_create OK£¬HE HE.\n";
        }
        $result = socket_connect($socket, $address, $service_port);
        if ($result < 0) {
            echo "SOCKETÁ¬½ÓÊ§°ÜÔ­Òò: ($result) " . socket_strerror($result) . "\n";
        } else {
            echo "OK.\n";
        }
        //·¢ËÍÃüÁî
        $in = $send_data;
        $out = '';
        echo "Send ..........";
        socket_write($socket, $in, strlen($in));
        echo "OK.\n";
        echo "Reading Backinformatin:\n\n";
        while ($out = socket_read($socket, 2048)) {
            echo $out;
        }
        echo "Close socket........";
        socket_close($socket);
        echo "OK,He He.\n\n";
        return $out;
    }








}